package com.mmlc.meimei.lichengkongjian.home;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ScrollView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.androidnetframe.utils.LogUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.BannerRequestFilter;
import com.mmlc.network.filter.GoodsListRequestFilter;
import com.mmlc.network.responsebean.BannerResponseBean;
import com.mmlc.network.responsebean.GoodsListResponseBean;
import com.mmlc.utils.AppUtils;
import com.mmlc.utils.ViewUtils;
import com.mmlc.widget.MyGridView;
import com.mmlc.widget.ViewBannerPager;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/7/14.
 */
public class ProductPreviewActivity extends InterActivity implements AdapterView.OnItemClickListener {
    @Override
    protected int initContentView() {
        return R.layout.activity_product_priview;
    }

    ViewBannerPager banner;
    PullToRefreshScrollView scv_home;
    MyGridView grid_product_preview;

    ArrayList<GoodsListResponseBean.DataBean.GoodslistBean> datas;
    ProductPreviewAdapter adapter;

    @Override
    protected void initView() {
        scv_home = ViewUtils.getView(activity, R.id.scv_home);
        banner = ViewUtils.getView(activity, R.id.banner);
        grid_product_preview = ViewUtils.getView(activity, R.id.grid_product_preview);

        datas = new ArrayList<>();
        adapter = new ProductPreviewAdapter(context, datas);
        grid_product_preview.setAdapter(adapter);
    }

    @Override
    protected void initInfo() {
        super.initInfo();
        getPreviewBanners();
        getPreviewProduct();
    }

    @Override
    protected void onResume() {
        super.onResume();
        scv_home.getRefreshableView().smoothScrollTo(0, 0);
    }

    private void getPreviewBanners() {
        BannerRequestFilter filter = new BannerRequestFilter(activity);
        filter.requestBean.page = "new_goods_list";
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<BannerResponseBean>() {
            @Override
            public void onSuccess(BannerResponseBean result) {
                LogUtils.i(TAG, result.toString());
                banner.setDatas(result.getData().getBanner().getAd_list());
                scv_home.getRefreshableView().scrollTo(0, 0);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                LogUtils.e(TAG, serverError.toString());
            }
        });
    }

    private void getPreviewProduct() {
        isOk = false;

        GoodsListRequestFilter filter = new GoodsListRequestFilter(activity);
        filter.requestBean.pageNow = String.valueOf(pageNow);
        filter.requestBean.is_new = "1";
        filter.requestBean.version = AppUtils.getVersionCode(this)+"";
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GoodsListResponseBean>() {
            @Override
            public void onSuccess(GoodsListResponseBean result) {
                if (pageNow == 0) datas.clear();
                datas.addAll(result.getData().getGoodslist());
                adapter.notifyDataSetChanged();

                isOk = true;
                scv_home.onRefreshComplete();
                if (pageNow == 0) scv_home.getRefreshableView().scrollTo(0, 0);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                isOk = true;
                scv_home.onRefreshComplete();
            }
        });
    }

    int pageNow = 0;
    boolean isOk = true;
    Handler refreshHandler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            if (msg.what == 0) {
                if (!isOk) {
                    scv_home.onRefreshComplete();
                    isOk = true;
                }
            }
        }
    };

    @Override
    protected void initEvent() {
        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);

        scv_home.setMode(PullToRefreshBase.Mode.BOTH);
        scv_home.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) throws InterruptedException {
                if (isOk) {
                    pageNow = 0;
                    getPreviewProduct();
                    refreshHandler.sendEmptyMessageDelayed(0, 3000);
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                if (isOk) {
                    pageNow++;
                    getPreviewProduct();
                    refreshHandler.sendEmptyMessageDelayed(0, 3000);
                }
            }
        });
        grid_product_preview.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(activity, NewProductInfoActivity.class);
        intent.putExtra("name", datas.get(position).getGoods_name())
                .putExtra("goods_id", datas.get(position).getId());
        startActivity(intent);
    }
}
